package com.xr.common.axios;

import java.util.HashMap;
import java.util.Objects;

public class R extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;

    private static final Integer HTTP_OK_CODE = 10000;

    private static final Integer HTTP_ERROR_CODE = 50000;

    private static final String OK_MSG = "执行成功";

    private static final String ERROR_MSG = "执行失败";

    public static final String CODE = "code";

    public static final String MSG = "msg";

    public static final String DATA = "data";

    public R put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 执行成功返回默认成功消息
     *
     * @return
     */
    public static R ok() {
        return Objects.requireNonNull(new R().put(CODE, HTTP_OK_CODE)).put(MSG, OK_MSG);
    }

    /**
     * 执行成功返回自定义成功消息
     *
     * @param msg
     * @return
     */
    public static R ok(String msg) {
        return Objects.requireNonNull(new R().put(CODE, HTTP_OK_CODE)).put(MSG, msg);
    }

    /**
     * 执行成功返回默认成功消息 和 默认Object（对应的key值为 "data"，调用时只需传入一个Object对象的参数）
     *
     * @param data
     * @return
     */
    public static R ok(Object data) {
        return Objects.requireNonNull(Objects.requireNonNull(new R().put(CODE, HTTP_OK_CODE)).put(MSG, OK_MSG)).put(DATA, data);
    }

    /**
     * 执行成功返回自定义成功消息 和 默认Object（对应的key值为 "data"，调用时只需传入一个Object对象的参数）
     *
     * @param msg
     * @param data
     * @return
     */
    public static R ok(String msg, Object data) {
        return Objects.requireNonNull(Objects.requireNonNull(new R().put(CODE, HTTP_OK_CODE)).put(MSG, msg)).put(DATA, data);
    }

    /**
     * 执行成功返回默认成功消息 和 自定义Object（对应的key值需标注）
     *
     * @return
     */
    public static R okResult() {
        return Objects.requireNonNull(new R().put(CODE, HTTP_OK_CODE)).put(MSG, OK_MSG);
    }

    /**
     * 执行成功返回自定义成功消息 和 自定义Object（对应的key值需标注）
     *
     * @return
     */
    public static R okResult(String msg) {
        return Objects.requireNonNull(Objects.requireNonNull(new R().put(CODE, HTTP_OK_CODE)).put(MSG, msg));
    }

    /**
     * 执行失败返回默认失败消息
     *
     * @return
     */
    public static R error() {
        return Objects.requireNonNull(new R().put(CODE, HTTP_ERROR_CODE)).put(MSG, ERROR_MSG);
    }

    /**
     * 执行失败返回自定义失败消息
     *
     * @param msg
     * @return
     */
    public static R error(String msg) {
        return Objects.requireNonNull(new R().put(CODE, HTTP_ERROR_CODE)).put(MSG, msg);
    }

}
